//
//  MessagePushViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/16.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "MessagePushViewController.h"
#import "TimeSetView.h"
@interface MessagePushViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray     *titleArr;
@property(nonatomic, strong) NSArray     *imageArr;
@property(nonatomic, strong) NSString    *timeStr;
@end

@implementation MessagePushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    lable.textColor     = [UIColor blackColor];
    lable.font          = SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text          = HHH_Text(@"消息推送");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    _tableView                 = [[UITableView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20)+44+2, HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20)-44-2) style:UITableViewStylePlain];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _imageArr = @[@"来电提醒", @"短信提醒", @"免打扰开关", @"应用提醒"];
    _titleArr = @[HHH_Text(@"来电提醒"), HHH_Text(@"短信提醒"), HHH_Text(@"免打扰开关"), HHH_Text(@"APP推送提醒")];
    _timeStr  = [NSString stringWithFormat:@"(%02ld:%02ld-%02ld:%02ld)", SN_USER.startH, SN_USER.startM, SN_USER.endH, SN_USER.endM];
    
    SNShadowView *shadowLine = [[SNShadowView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
    [shadowLine showWithType:1];
    [self.view addSubview:shadowLine];
    
    UIButton *checkBtn       = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBtn.frame = CGRectMake(HHHWIDTH/8,HHHHEIGHT - FitH(40) - 30, HHHWIDTH/8*6, FitH(40));
    [checkBtn setBackgroundColor:[UIColor blackColor]];
    [checkBtn setTitleNormal:HHH_Text(@"一键检查推送异常")];
    checkBtn.titleLabel.font = SNFONT(FitFont(20));
    [checkBtn setTitleColorNormal:[UIColor whiteColor]];
    checkBtn.layer.cornerRadius = 5;
    checkBtn.clipsToBounds      = YES;
    [checkBtn addTouchUpTarget:self action:@selector(checkUpdate)];
//    [self.view addSubview: checkBtn];
}

- (void)checkUpdate
{
//    [KK_BLESend sendUserPairWithBlock:^(id object, KKBLEAcceptType type) {
//
//    }];
    [KK_BLESend sendUserInfoSettingWithBlock:^(id object, KKBLEAcceptType type) {
        
    }];
}

- (void)leftbuttonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return FitH(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell                           = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle            = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.textColor = UIColorHEX(0x727272);
    }
    cell.imageView.image              = UIImageNamed(_imageArr[indexPath.row]);
    cell.imageView.layer.cornerRadius = cell.imageView.height/2;
    cell.imageView.clipsToBounds      = YES;
    cell.textLabel.text               = _titleArr[indexPath.row];
    cell.textLabel.font               = SNFONT(15);
    
    ZJSwitch *btn = [[ZJSwitch alloc] initWithFrame:CGRectMake(HHHWIDTH-50, FitH(10), 50, 22)];
    btn.tag = 100 +indexPath.row;
    [btn addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = btn;

    if (indexPath.row == 2) {
        UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        setBtn.frame = CGRectMake(FitW(334)-FitW(50), FitH(5), FitW(50), FitH(60)-FitH(10));
        [setBtn setImageNormal:@"设置1"];
        setBtn.tag = 100+indexPath.row;
        [setBtn addTouchUpTarget:self action:@selector(setBtnClick:)];
        [cell addSubview:setBtn];
        cell.detailTextLabel.text = _timeStr;
        
        SNShadowView *shadowLine1 = [[SNShadowView alloc] init];
        shadowLine1.frame = SNFRAME(0, 0, HHHWIDTH, 0);
        [shadowLine1 showWithType:1];
        [cell addSubview:shadowLine1];
        
        SNShadowView *shadowLine2 = [[SNShadowView alloc] init];
        shadowLine2.frame = SNFRAME(0, FitH(60) -1, HHHWIDTH, 0);
        [shadowLine2 showWithType:1];
        [cell addSubview:shadowLine2];
    }
    if (indexPath.row == 0) {
        btn.on = SN_USER.phoneNotice;
    } else if (indexPath.row == 1) {
        btn.on = SN_USER.smsNotice;
    } else if (indexPath.row == 2) {
        btn.on = SN_USER.isOpen;
    } else {
        btn.on = SN_USER.ancsNotice;
    }
    return cell;
}

- (void)setBtnClick:(UIButton *)sender {
    TimeSetView *setView = [[TimeSetView alloc]initWithFrame:self.view.bounds withType:IgnoreAll];
    
    [self.view addSubview:setView];
    [setView animationUp];
    setView.sureButtonBlock = ^(NSString *str) {
        _timeStr = str;
        NSIndexPath *indexPath  = [NSIndexPath indexPathForRow:2 inSection:0];
        NSArray     *indexArray = [NSArray arrayWithObject:indexPath];
        [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone];
    };
}

- (void)handleSwitchEvent:(ZJSwitch *)sender {
    switch (sender.tag) {
        case 100:
            SN_USER.phoneNotice = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 101:
            SN_USER.smsNotice = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 102:
            SN_USER.isOpen = sender.on;
            [KK_BLESend sendNotDisturbWithBlock:^(id object, KKBLEAcceptType type) {
            }];
            break;
        case 103:
            SN_USER.ancsNotice = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        default:
            break;
    }
    [SN_USER updateToDB];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

